package database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PurchaseItemTable extends Table {

	private static final String PURCHASE_ITEM_INSERT = "INSERT INTO PurchaseItem VALUES (%s, %s, %s)";
	private static final String PURCHASE_ITEM_DELETE = "DELETE FROM PurchaseItem WHERE receiptId=%s AND upc=%s";
	private static final String PURCHASE_ITEM_DISPLAY = "SELECT * FROM PurchaseItem";
	
	private static final String PURCHASE_ITEM_SIZE = "SELECT COUNT(receiptId) FROM PurchaseItem";
	
	private static final String PURCHASE_ITEM_EXISTS_QUERY = "SELECT * FROM PurchaseItem WHERE receiptId=%s AND upc=%s";
	
	private static final String CUR_RECEIPTID_QUERY = "SELECT receiptId_counter.currval from dual";
	
	public enum Column
	{
		RECEIPT_ID(1, true),
		UPC(2, true),
		QUANTITY(3, false);
		
		private int index;
		private boolean isPrimaryKey;
		
		private String[] columnNames = {
				"receiptId",
				"upc",
				"quantity"};
		
		private Column(int index, boolean isPrimaryKey) 
		{
			this.index = index;
			this.isPrimaryKey = isPrimaryKey;
		}
		
		public int getIndex()
		{
			return index;
		}
		
		public String toString()
		{
			return columnNames[this.ordinal()];
		}

		public boolean isPrimaryKey() {
			return this.isPrimaryKey;
		}
	};
	
	public PurchaseItemTable()
	{
		super(PURCHASE_ITEM_INSERT, PURCHASE_ITEM_DELETE, PURCHASE_ITEM_DISPLAY, PURCHASE_ITEM_EXISTS_QUERY);
	}

	@Override
	public List<String> getPrimaryKeyValues(List<String> insertValues) {
		List<String> primaryKeyValues = new ArrayList<String>();
		for(Column col : Column.values())
		{
			if(col.isPrimaryKey())
			{	
				primaryKeyValues.add(insertValues.get(col.ordinal()));
			}
		}				
		
		return primaryKeyValues;
	}
	
	@Override
	public List<String> getColumnNames() {
		Column columns[] = Column.values();
		
		List<String> columnNames = new ArrayList<String>(columns.length);
		for( Column col : columns)
		{
			columnNames.add(col.ordinal(), col.toString());
		}
		return columnNames;
	}

	@Override
	public List<String> getPrimaryKeyColumnNames() {
		Column columns[] = Column.values();
		
		List<String> primaryKeyColumnNames = new ArrayList<String>(columns.length);
		for( Column col : columns)
		{
			if(col.isPrimaryKey())
			{
				primaryKeyColumnNames.add(col.ordinal(), col.toString());
			}
		}
		return primaryKeyColumnNames;
	}
	
	public void purchaseItem(String upc, String quantity) throws SQLException 
	{
		List<String> purchaseTuple = new ArrayList<String>(Column.values().length);
		int receiptId = getCurReceiptId();
		
		purchaseTuple.add(Column.RECEIPT_ID.ordinal(), String.valueOf(receiptId));
		purchaseTuple.add(Column.UPC.ordinal(), upc);
		purchaseTuple.add(Column.QUANTITY.ordinal(), quantity);
		
		insert(purchaseTuple);
	}
	
	public void returnPurchasedItem(String receiptId, String upc) throws SQLException 
	{
		List<String> returnTuple = new ArrayList<String>(Column.values().length);
		returnTuple.add(Column.RECEIPT_ID.ordinal(), receiptId);
		returnTuple.add(Column.UPC.ordinal(), upc);
		
		delete(returnTuple);
	}
	
	public int getItemsLeft() throws SQLException {
		int curNumberItems = 0;
		
		ResultSet rs = dbCon.executeQuery(PURCHASE_ITEM_SIZE);
		if(rs != null)
		{
			if(rs.next())
			{
				curNumberItems = rs.getInt(1);
			}
			
			rs.close();
		}
				
		return curNumberItems;
	}
	
	private int getCurReceiptId() throws SQLException
	{
		int receiptId = 0;
		ResultSet rs = dbCon.executeQuery(CUR_RECEIPTID_QUERY);
		
		if(rs != null)
		{
			if(rs.next())
			{
				receiptId = rs.getInt(1);
			}
		}
		
		return receiptId;
	}
	
	
}
